package com.greyfit.security

import org.springframework.dao.DataIntegrityViolationException

class GreyFitUserController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [greyFitUserInstanceList: GreyFitUser.list(params), greyFitUserInstanceTotal: GreyFitUser.count()]
    }

    def create() {
        [greyFitUserInstance: new GreyFitUser(params)]
    }

    def save() {
        def greyFitUserInstance = new GreyFitUser(params)
        if (!greyFitUserInstance.save(flush: true)) {
            render(view: "create", model: [greyFitUserInstance: greyFitUserInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), greyFitUserInstance.id])
        redirect(action: "show", id: greyFitUserInstance.id)
    }

    def show(Long id) {
        def greyFitUserInstance = GreyFitUser.get(id)
        if (!greyFitUserInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), id])
            redirect(action: "list")
            return
        }

        [greyFitUserInstance: greyFitUserInstance]
    }

    def edit(Long id) {
        def greyFitUserInstance = GreyFitUser.get(id)
        if (!greyFitUserInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), id])
            redirect(action: "list")
            return
        }

        [greyFitUserInstance: greyFitUserInstance]
    }

    def update(Long id, Long version) {
        def greyFitUserInstance = GreyFitUser.get(id)
        if (!greyFitUserInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (greyFitUserInstance.version > version) {
                greyFitUserInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'greyFitUser.label', default: 'GreyFitUser')] as Object[],
                          "Another user has updated this GreyFitUser while you were editing")
                render(view: "edit", model: [greyFitUserInstance: greyFitUserInstance])
                return
            }
        }

        greyFitUserInstance.properties = params

        if (!greyFitUserInstance.save(flush: true)) {
            render(view: "edit", model: [greyFitUserInstance: greyFitUserInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), greyFitUserInstance.id])
        redirect(action: "show", id: greyFitUserInstance.id)
    }

    def delete(Long id) {
        def greyFitUserInstance = GreyFitUser.get(id)
        if (!greyFitUserInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), id])
            redirect(action: "list")
            return
        }

        try {
            greyFitUserInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'greyFitUser.label', default: 'GreyFitUser'), id])
            redirect(action: "show", id: id)
        }
    }
}
